using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core.Graphics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.IO;
using Core;

namespace WorldEditor
{
    public partial class AddTileSet : Form
    {
        GraphicsDevice _Device;
        DirectoryInfo _TerrainDirectory;
        TileSetTypes _TileSetType;

        public AddTileSet(GraphicsDevice device, DirectoryInfo TextureDirectory, TileSetTypes settype)
        {
            _Device = device;
            _TerrainDirectory = TextureDirectory;
            _TileSetType = settype;

            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowseTileSet_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Get File Name and place in Textbox
                txtTileSetFile.Text = openFileDialog1.FileName;
                //Change Accept Button to Create;
                this.AcceptButton = btnCreate;
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            //Get Filename
            string filename = openFileDialog1.FileName;
            //Load texture from file
            Texture2D text = Texture2D.FromFile(_Device, filename);
            //Create new TileSet
            TileSet ts = new TileSet(TileSetCollection.NextID, text, txtName.Text, Convert.ToInt32(tilewidth.Value),
                Convert.ToInt32(tileheight.Value), Convert.ToInt32(horizontalspacing.Value),
                Convert.ToInt32(verticalspacing.Value));
            //Add TileSet to Master Collection for it's type
            TileSetCollection tsc = TileSetLoaderHelper.GetCollectionForType(_TileSetType);
            tsc.Add(ts.ID, ts);
            TileSetCollection.NextID = ts.ID;
            //Now serialize it
            TileSetLoaderHelper.SaveTileSet(ts, _TerrainDirectory.FullName, filename, _TileSetType);
            //Exit Add TileSet Window after we have added it
            this.Close();
        }
    }
}